PhpFpm   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 6
eloc 48
dl 0
loc 61
rs 10
c 0
b 0
f 0

5 Functions

Rating   Name   Duplication   Size   Complexity  
A updateConfiguration 0 15 1
A configure 0 5 1
A addPerformanceConfiguration 0 10 2
A linkPhpVersion 0 2 1
A unLinkPhpVersion 0 2 1
1
import * as fs from 'fs'
2
import * as os from 'os'
3
import OS from '../client/OS'
4
import zPerformanceIni from '../templates/zPerformanceIni'
5
import {ensureDirectoryExists} from '../utils/filesystem'
6
import {jaleHomeDir, jaleLogsPath} from '../utils/jale'
7
import Service from './service'
8
9
abstract class PhpFpm extends Service {
10
    requireRoot = true
11
    isEndOfLife = false
12
13
    abstract versionName: string
14
15
    abstract configPath: string
16
    abstract iniDirectoryPath: string
17
18
    configRootPath = `${OS.getInstance().usrLocalDir}/etc/php`
19
20
    configure = async (): Promise<boolean> => {
21
        await this.updateConfiguration()
22
        await this.addPerformanceConfiguration()
23
24
        return true
25
    }
26
27
28
    /**
29
     * Update Php's www.conf configuration.
30
     */
31
    updateConfiguration = async (): Promise<void> => {
32
        let config: string = await fs.readFileSync(this.configPath, 'utf-8')
33
34
        config = config.replace(/^user = .+$/m, `user = ${os.userInfo().username}`)
35
        config = config.replace(/^group = .+$/m, 'group = staff') // TODO: Make this dynamic. GIDs dont work.
36
        config = config.replace(/^listen = .+$/m, `listen = ${jaleHomeDir}/jale.sock`)
37
        config = config.replace(/^;?listen\.owner = .+$/m, `listen.owner = ${os.userInfo().username}`)
38
        config = config.replace(/^;?listen\.group = .+$/m, 'listen.group = staff') // TODO: Make this dynamic. GIDs dont work.
39
        config = config.replace(/^;?listen\.mode = .+$/m, 'listen.mode = 0777')
40
        config = config.replace(
41
            /^;?php_admin_value\[error_log\] = .+$/m,
42
            `php_admin_value[error_log] = ${jaleLogsPath}/php.log`
43
        )
44
45
        return fs.writeFileSync(this.configPath, config)
46
    }
47
48
    /**
49
     * Create the z-performance.ini file which contains some optimized config settings.
50
     */
51
    addPerformanceConfiguration = async (): Promise<void> => {
52
        await ensureDirectoryExists(this.iniDirectoryPath)
53
54
        const path = `${this.iniDirectoryPath}/z-performance.ini`
55
56
        if (fs.existsSync(path)) {
57
            return
58
        }
59
60
        return fs.writeFileSync(path, zPerformanceIni)
61
    }
62
63
    unLinkPhpVersion = async (): Promise<boolean> => {
64
        return OS.getInstance().serviceCtl.unlink(this.service)
65
    }
66
67
    linkPhpVersion = async (): Promise<boolean> => {
68
        return OS.getInstance().serviceCtl.link(this.service)
69
    }
70
}
71
72
export default PhpFpm
73